package com.ruoyi.web.controller.crm;

import com.bonuo.crm.domain.ProductInfo;
import com.bonuo.crm.service.ProductInfoService;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.config.ServerConfig;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 产品信息Controller
 */
@Api(value = "产品信息")
@RestController
@RequestMapping("/crm/product")
public class ProductInfoController extends BaseController {
    @Autowired
    private ProductInfoService productInfoService;
    @Autowired
    private ServerConfig serverConfig;
    /**
     * 查询产品信息列表
     */
    @ApiOperation(value = "查询产品信息列表",httpMethod = "GET")
    @GetMapping("/list")
    public TableDataInfo list(ProductInfo productInfo, Integer pageNum, Integer pageSize) {
        if(pageNum!=null && pageSize!=null){
            startPage();
        }
        List<ProductInfo> list = productInfoService.selectProductInfoList(productInfo);
        return getDataTable(list);
    }

    /**
     * 导出产品信息列表
     */
    @ApiOperation(value = "导出产品信息列表",httpMethod = "POST")
    @PostMapping("/export")
    public void export(HttpServletResponse response, ProductInfo productInfo) {
        List<ProductInfo> list = productInfoService.selectProductInfoList(productInfo);
        ExcelUtil<ProductInfo> util = new ExcelUtil<>(ProductInfo.class);
        util.exportExcel(response, list, "产品信息数据");
    }

    /**
     * 新增产品信息
     */
    @ApiOperation(value = "新增产品信息",httpMethod = "POST")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody ProductInfo productInfo) {
        return toAjax(productInfoService.insertProductInfo(productInfo));
    }

    /**
     * 修改产品信息
     */
    @ApiOperation(value = "修改产品信息",httpMethod = "PUT")
    @PutMapping("/update")
    public AjaxResult update(@RequestBody ProductInfo productInfo) {
        return toAjax(productInfoService.updateProductInfo(productInfo));
    }

    /**
     * 删除产品信息
     */
    @ApiOperation(value = "删除产品信息",httpMethod = "DELETE")
    @DeleteMapping("/delete/{id}")
    public AjaxResult delete(@PathVariable Integer id) {
        return toAjax(productInfoService.deleteProductInfoById(id));
    }

    /**
     * 上传产品图片
     */
    @ApiOperation(value = "上传产品图片",httpMethod = "POST")
    @PostMapping("/uploadImage")
    public AjaxResult uploadImage(@RequestParam("file") MultipartFile file) {
        try {
            if (!file.isEmpty()) {
                // 上传文件路径
                String filePath = RuoYiConfig.getUploadPath();
                // 上传并返回新文件名称
                String fileName = FileUploadUtils.upload(filePath, file);
                String url = serverConfig.getUrl() + fileName;
                
                AjaxResult ajax = AjaxResult.success();
                ajax.put("fileName", fileName);
                ajax.put("url", url);
                return ajax;
            }
            return AjaxResult.error("上传文件不能为空");
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }
}